package com.sms.monitor.ui.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.sms.monitor.R;
import com.sms.monitor.constant.Constant;

/**
 * 短信弹出框,支持显示联系人姓名
 * 
 * @author dongfang
 * 
 */

public class Dialog2Activity extends Activity implements OnClickListener {
    private Button mClose;

    private TextView tvToFrom;
    private TextView tvToFromPersons;// 发件人或收件人
    private TextView tvToFromMsg; // 短信内容
    private Timer mTimer;
    private static final int DELAY = 30 * 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_2);
        findViews();
        init();
    }

    @SuppressLint("NewApi")
    private void findViews() {
        mClose = (Button) findViewById(R.id.btn_close);

        tvToFrom = (TextView) findViewById(R.id.tv_to_from);
        tvToFromPersons = (TextView) findViewById(R.id.tv_to_from_persons);
        tvToFromMsg = (TextView) findViewById(R.id.tv_to_from_msg);

        if (Build.VERSION.SDK_INT > 10) {
            setFinishOnTouchOutside(false);
        }
    }

    private void init() {
        Bundle bundle = getIntent().getExtras();
        // 发送短信
        if (bundle!= null && 1 == bundle.getInt(Constant.KEY_SMS_TYPE)) {
            tvToFrom.setText("TO:");
            tvToFromPersons.setText(bundle.getString(Constant.KEY_SMS_SEND_PHONE));
            tvToFromMsg.setText(Html.fromHtml(bundle.getString(Constant.KEY_SMS_MSG)));
            tvToFromMsg.setMovementMethod(LinkMovementMethod.getInstance());
        }
        // 接收短信
        else {
            tvToFrom.setText("FROM:");
            tvToFromPersons.setText(bundle.getString(Constant.KEY_SMS_SEND_PHONE));
            tvToFromMsg.setText(Html.fromHtml(bundle.getString(Constant.KEY_SMS_MSG)));
            tvToFromMsg.setMovementMethod(LinkMovementMethod.getInstance());
        }
        mClose.setOnClickListener(this);
        mTimer = new Timer();
        // 30秒定时关闭对话框。
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                //
                finish();
            }
        }, DELAY);
    }

    /**
     * 关闭按钮事件
     */
    @Override
    public void onClick(View v) {
        //
        switch (v.getId()) {
        case R.id.btn_close:
            finish();
            break;

        default:
            break;
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (mTimer != null) {
            mTimer.cancel();
        }
    }
}
